//
//  ReadEverListViewController.m
//  product_B
//
//  Created by lanou on 16/5/30.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ReadEverListViewController.h"
#import "ReadDateListViewController.h"
@interface ReadEverListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dateArray;
@property (nonatomic,strong)UIView *bigView;
@property (nonatomic,strong)UIView *smallView;
@property (nonatomic,assign)NSInteger index;
@property (nonatomic,strong)NSMutableArray *modelArray;
@end

@implementation ReadEverListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.hidden = NO;;
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight + 49 - 64 )];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.dateArray = [NSMutableArray array];
    [self setDatearray];
    self.bigView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, kWidth, 50)];
    self.navigationItem.title = @"往期列表";
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    button1.frame = CGRectMake(kWidth * 0.15, 10, 40, 25);
    [button1 setTitle:@"短篇" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    button1.tag = 1;
    [self.bigView addSubview:button1];
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    button2.frame = CGRectMake(kWidth * 0.45, 10, 40, 25);
    [button2 setTitle:@"连载" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    button2.tag = 2;
    [self.bigView addSubview:button2];
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];
    button3.frame = CGRectMake(kWidth * 0.75, 10, 40, 25);
    [button3 setTitle:@"问题" forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    button3.tag = 3;
    self.smallView = [[UIView alloc]initWithFrame:CGRectMake(kWidth * 0.15, 47, 40, 2)];
    self.smallView.backgroundColor = [UIColor cyanColor];
    [self.bigView addSubview:self.smallView];
    [self.bigView addSubview:button3];
    self.tableView.tableHeaderView = self.bigView;
    
}


- (void)buttonAction:(UIButton *)sender{
    [UIView animateWithDuration:0.5 animations:^{
        self.smallView.frame = CGRectMake(sender.frame.origin.x, 47, 40, 2);
    }];
    self.index = sender.tag - 1;
    [self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (void)setDatearray{
    NSString *string = @"本月";
    NSInteger year = [[[self dateString] substringToIndex:4]intValue];
    NSInteger mouth = [[[self dateString] substringFromIndex:5]intValue];
    NSMutableArray *esseyArray = [NSMutableArray array];
    NSMutableArray *questionArray = [NSMutableArray array];
    NSMutableArray *serialArray = [NSMutableArray array];
    [esseyArray addObject:string];
    [questionArray addObject:string];
    [serialArray addObject:string];
    for (int i = (int)mouth - 1; i>0; i--) {
        NSString *string = [NSString stringWithFormat:@"%ld-%.2d",year,i];
        [esseyArray addObject:string];
        [questionArray addObject:string];
        [serialArray addObject:string];
    }
    for (int i = (int)year - 1; i>=2013; i--) {
        for (int j = 12; j>0; j--) {
            NSString *string = [NSString stringWithFormat:@"%d-%.2d",i,j];
            [esseyArray addObject:string];
            [questionArray addObject:string];
        }
    }
    [self.dateArray addObject:esseyArray];
    [self.dateArray addObject:serialArray];
    [self.dateArray addObject:questionArray];
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (NSString *)dateString{
    NSDate *date = [[NSDate alloc]init];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"YYYY-MM";
    NSString *dateString = [formatter stringFromDate:date];
    return dateString;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    self.modelArray = self.dateArray[_index];
    return self.modelArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.text = self.modelArray[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ReadDateListViewController *dateVC = [[ReadDateListViewController alloc]init];
    dateVC.index = _index;
    if (_index == 0) {
        dateVC.url = [NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/essay/bymonth/%@",self.modelArray[indexPath.row]];
    }else if (_index == 1){
        dateVC.url = [NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/serialcontent/bymonth/%@",self.modelArray[indexPath.row]];
    }else{
        dateVC.url = [NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/question/bymonth/%@",self.modelArray[indexPath.row]];
    }
    [self.navigationController pushViewController:dateVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
